#include "penvisualwidget.h"
#include <QPainter>

PenVisualWidget::PenVisualWidget(QWidget *parent) :
    QWidget(parent)
{
    this->repaint();
    //Specify initial pen settings
    m_Color = Qt::black;
    m_Width = 5;
    m_Wwidth = 200;
    m_Wheight = 100;
}

//repaint() event
void PenVisualWidget::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  {
    QPen pen = painter.pen();
    pen.setCapStyle(Qt::RoundCap);
    pen.setColor(m_Color);
    pen.setWidth(m_Width);
    painter.setPen(pen);
  }

  //Paint according to current window size
  painter.drawLine(50,(m_Wheight/2),(m_Wwidth-50),(m_Wheight/2));

  emit changePen(m_Color, m_Width);
}

//Set colors from GUI sliders
void PenVisualWidget::setRed(int red)
{
    m_Color.setRed(red);
    m_Red = red;
    this->repaint();
}

void PenVisualWidget::setGreen(int green)
{
    m_Color.setGreen(green);
    m_Green = green;
    this->repaint();
}

void PenVisualWidget::setBlue(int blue)
{
    m_Color.setBlue(blue);
    m_Blue = blue;
    this->repaint();
}

//Set width from GUI slider
void PenVisualWidget::setWidth(int width)
{
    m_Width = width;
    this->repaint();
}

//Set colors from GUI combo box
void PenVisualWidget::setColorCB(int index)
{
    switch(index)
    {
        case 0: {
            m_Color = Qt::black;
        }
        break;
        case 1: {
            m_Color = Qt::red;
        }
        break;
        case 2: {
            m_Color = Qt::green;
        }
        break;
        case 3: {
            m_Color = Qt::blue;

        }
        break;
        case 4: {
            m_Color = Qt::yellow;

        }
        break;
        case 5: {
            m_Color = Qt::gray;

        }
        break;
        case 6: {
            m_Color = Qt::white;
        }
        break;
    }
    this->repaint();
 }

//Set width from GUI combo box
void PenVisualWidget::setWidthCB(int index)
{
    switch(index)
    {
        case 0: {
            m_Width = 5;

        }
        break;
        case 1: {
            m_Width = 10;

        }
        break;
        case 2: {
            m_Width = 20;

        }
    }
    this->repaint();
}

void PenVisualWidget::resizeEvent(QResizeEvent *)
{
    QSize size;
    size = this->size();
    m_Wwidth = size.width();
    m_Wheight = size.height();
}
